package com.fan.shop.views.bean;

import java.io.File;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;

import org.apache.log4j.Logger;

import com.fan.shop.businesslogic.BrandCreateProcess;
import com.fan.shop.businesslogic.BrandUpdateProcess;
import com.fan.shop.businesslogic.FileUploadProcess;
import com.fan.shop.domain.Brand;
import com.fan.shop.global.ShopGlobalUtils;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.services.BrandService;

@ManagedBean(name = "editBrandBean")
@RequestScoped
public class EditBrandBean extends ShopBean {

	private static Logger logger = Logger.getLogger(EditBrandBean.class);

	@ManagedProperty(value = "#{brandService}")
	private BrandService brandService;

	private Part iconFile;
	private String previewIconPath;
	private String tempIconFilePath;

	private String name;
	private String description;
	private String icon;
	private boolean enabled;
	private int enableFlag;

	private int mode;

	public EditBrandBean() {
		super();

		if (mode == ShopGlobalVariables.EDIT_MODE) {
			loadBrand();
		} else {
			if (FacesContext.getCurrentInstance().getExternalContext()
					.getRequestMap().get("brand_name") != null) {
				name = FacesContext.getCurrentInstance().getExternalContext()
						.getRequestMap().get("brand_name").toString();
			}
		}

	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		EditBrandBean.logger = logger;
	}

	public BrandService getBrandService() {
		return brandService;
	}

	public void setBrandService(BrandService brandService) {
		this.brandService = brandService;
	}

	public Part getIconFile() {
		return iconFile;
	}

	public void setIconFile(Part iconFile) {
		this.iconFile = iconFile;
	}

	public String getPreviewIconPath() {
		return previewIconPath;
	}

	public void setPreviewIconPath(String previewIconPath) {
		this.previewIconPath = previewIconPath;
	}

	public String getTempIconFilePath() {
		return tempIconFilePath;
	}

	public void setTempIconFilePath(String tempIconFilePath) {
		this.tempIconFilePath = tempIconFilePath;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public boolean isEnabled() {
		if (enableFlag == 1) {
			enabled = true;
		} else {
			enabled = false;
		}
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public int getEnableFlag() {
		return enableFlag;
	}

	public void setEnableFlag(int enableFlag) {
		this.enableFlag = enableFlag;
	}

	public int getMode() {
		return mode;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	public void loadBrand() {
		if (name != null && name.trim().length() > 0) {
			Brand brand = brandService.getBrand(name);
			description = brand.getDescription();
			icon = brand.getIcon();
			if (brand.getEnabled() == 0) {
				enabled = false;
				enableFlag = 0;
			} else {
				enabled = true;
				enableFlag = 1;
			}
		}
	}

	public String loadIconAction() {
		FileUploadProcess fileUploadProcess = new FileUploadProcess();
		fileUploadProcess.setFileName("temp");
		fileUploadProcess.setPart(iconFile);
		fileUploadProcess.setUploadType(ShopGlobalVariables.TEMP);
		fileUploadProcess.setFileContentType(ShopGlobalUtils
				.getFileContentType(previewIconPath));
		int result = fileUploadProcess.process();
		if (result == ShopGlobalVariables.SUCCESS) {
			previewIconPath = fileUploadProcess.getRelativePath();
			icon = previewIconPath;
		} else {
			System.out.println(icon);
		}

		tempIconFilePath = fileUploadProcess.getFullFilePath();

		return "edit_brand.faces";
	}

	public String submitAction() {
		if (mode == ShopGlobalVariables.EDIT_MODE) {
			BrandUpdateProcess brandUpdateProcess = new BrandUpdateProcess();
			brandUpdateProcess.setBrandService(brandService);
			Brand brand = brandService.getBrand(name);
			brand.setDescription(description);
			brand.setIcon(icon);
			if (enabled) {
				enableFlag = 1;
			} else {
				enableFlag = 0;
			}
			brand.setEnabled(enableFlag);
			brandUpdateProcess.setBrand(brand);
			brandUpdateProcess.setIconTempFile(new File(tempIconFilePath));

			int result = brandUpdateProcess.process();
			if (result == ShopGlobalVariables.FAILED) {
				logger.error(brandUpdateProcess.getErrorMessage());
				return "edit_brand.faces";
			} else {

				return "brands.faces?faces-redirect=true";
			}
		} else {
			BrandCreateProcess brandCreateProcess = new BrandCreateProcess();
			brandCreateProcess.setBrandService(brandService);
			Brand brand = new Brand();
			brand.setName(name);
			brand.setDescription(description);
			brand.setIcon(icon);
			if (enabled) {
				enableFlag = 1;
			} else {
				enableFlag = 0;
			}
			brand.setEnabled(enableFlag);
			brandCreateProcess.setBrand(brand);
			brandCreateProcess.setIconTempFile(new File(tempIconFilePath));

			int result = brandCreateProcess.process();
			if (result == ShopGlobalVariables.FAILED) {
				logger.error(brandCreateProcess.getErrorMessage());
				return "edit_brand.faces";
			} else {

				return "brands.faces?faces-redirect=true";
			}
		}

	}

	public String backToItemsAction() {
		return "brands.faces?faces-redirect=true";
	}
}
